using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using M0;
using UnityEngine;
using UnityEngine.UI;

public class 获得奖励动画 : MonoBehaviour
{
    private static 获得奖励动画 instance;

    public static 获得奖励动画 Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject obj = new GameObject("获得奖励动画", typeof(获得奖励动画));
                instance = obj.GetComponent<获得奖励动画>();

                var canvas = FindObjectOfType<CanvasScaler>();
                instance.transform.SetParent(canvas.transform,false);
                DontDestroyOnLoad(obj.gameObject);
            }
            
            return instance;
        }
    }

    public async void SetUpPlay动画(Transform from, Transform 目标, GameObject 复制哪个, List<Sprite> 化妆, List<Vector2> 化妆大小, Action<int,int> 更新call, Action 回收call)
    {
        if (化妆.Count == 0 || 复制哪个 == null)
        {
            Debug.LogWarning(化妆.Count+" "+复制哪个);
            更新call?.Invoke(1,1);
            回收call?.Invoke();
            return;
        }
        
        gameObject.SetActive(true);
        
        var copy = GameObject.Instantiate(复制哪个, transform);
        copy.transform.position = 复制哪个.transform.position;
        copy.transform.SetAsFirstSibling();
        var rect = 复制哪个.GetComponent<RectTransform>();
        var rect2 = copy.GetComponent<RectTransform>();
        
        rect2.sizeDelta = rect.sizeDelta;
        // if (rect2 != null)
        // {
        //     GameObject.Destroy(rect2);
        //     rect2 =copy.AddComponent<RectTransform>();
        //     
        // }
        
        
        //按钮移除
        var allbutton = copy.GetComponentsInChildren<GameButton>();
        foreach (var VARIABLE in allbutton)
        {
            Destroy(VARIABLE);
        }


        List<Image> 这波的Image = new List<Image>();
        int i = 0;
        foreach (var VARIABLE in 化妆)
        {
            var img = GetImg();
            img.sprite = VARIABLE;
            img.rectTransform.sizeDelta = 化妆大小[i];
            img.transform.position = from.position;
            这波的Image.Add(img);
            i++;
        }
        
        //周围扩散

        Tweener 最后一个动画=null;
        int index = 0;
        foreach (var VARIABLE in 这波的Image)
        {
            
            var 随机坐标 = v2的随机坐标(VARIABLE.rectTransform);
            var cur坐标 = VARIABLE.rectTransform.anchoredPosition;
            
            
            index++;
            if (index < 这波的Image.Count)
            {
                M.异步动画(cur坐标,随机坐标,0.25f, (v) =>
                {
                    VARIABLE.rectTransform.anchoredPosition = v;
                },null);
                
                await M.异步等待(0.1f);
                // await KnownCommandOptions.Common.异步等待(0.1f);
            }
            else
            {
                M.异步动画(cur坐标,随机坐标,0.25f, (v) =>
                {
                    VARIABLE.rectTransform.anchoredPosition = v;
                }, () =>
                {
                    var 距离 = Vector3.Distance(这波的Image[0].transform.position, 目标.position);
                    var 第一个人的速度 = 距离 / 0.5f;
                    int i = 0;
                    foreach (var UPPER in 这波的Image)
                    {
                        Vector3 selfposition = UPPER.transform.position;
                        
                        var self距离 = Vector3.Distance(selfposition, 目标.position);
                        var 相同的速度 = self距离 / 第一个人的速度;
                        i++;
                        int curI = i;

                        if (i < 这波的Image.Count)
                        {
                            M.异步动画(selfposition,目标.position,相同的速度, (v) =>
                            {
                                UPPER.transform.position = v;
                            }, () =>
                            {
                                更新call?.Invoke(curI,这波的Image.Count);
                                UPPER.gameObject.SetActive(false);
                            });
                        }
                        else
                        {
                            M.异步动画(selfposition,目标.position,相同的速度, (v) =>
                            {
                                UPPER.transform.position = v;
                            }, () =>
                            {
                                更新call?.Invoke(curI,这波的Image.Count);
                                回收call?.Invoke();
                                UPPER.gameObject.SetActive(false);
                                
                                Destroy(copy);

                                waitAndClear(这波的Image);
                                gameObject.SetActive(false);
                            });
                        }
                        
                        
                    }
                });
                break;
            }
        }
    }

    private async void waitAndClear(List<Image> 这波的Image)
    {
        await Task.Yield();
        foreach (var VARIABLE in 这波的Image)
        {
            Mono缓存池.Instance.F回收("获得奖励动画_img",VARIABLE.gameObject);
        }
    }

    private Image 模版;
    private Image GetImg()
    {
        if (模版 == null)
        {
            模版 = new GameObject("Img", typeof(Image)).GetComponent<Image>();
        }

        var 缓存Img = Mono缓存池.Instance.F创建<Image>("获得奖励动画_img", 模版, transform);
        return 缓存Img;
    }
    Vector2 v2的随机坐标(RectTransform rect)
    {
        var size = rect.sizeDelta;
        var pos = rect.anchoredPosition;

        var randomx = UnityEngine.Random.Range(size.x * -1, size.x * 1);
        var randomy = UnityEngine.Random.Range(size.y * -1, size.y * 1);
        return pos + new Vector2(randomx, randomy);
    }
}
